package com.heima.wemedia.controller.v1;

import com.heima.api.wemedia.WmNewsControllerApi;
import com.heima.common.constants.WemediaContants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author feng js
 */
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    WmNewsService wmNewsService;

    @PostMapping("/list")
    @Override
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAll(wmNewsPageReqDto);
    }
    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNewsDto) {
        if (wmNewsDto.getStatus().equals(WmNews.Status.SUBMIT.getCode())) {
            return wmNewsService.submitNews(wmNewsDto, WmNews.Status.SUBMIT.getCode());
        } else {
            return wmNewsService.submitNews(wmNewsDto,WmNews.Status.NORMAL.getCode());
        }

    }
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findById(@PathVariable("id") Integer id) {

       return wmNewsService.findById(id);
    }
    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delNewsById(@PathVariable("id") Integer id) {

        return wmNewsService.delNewsById(id);
    }


    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto wmNewsDto) {
        return wmNewsService.downOrUp(wmNewsDto);
    }

    @GetMapping("/findOne/{id}")
    @Override
    public WmNews findByIdWmNews(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }
    @PostMapping("/update")
    @Override
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
         wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/findRelease")
    @Override
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    @PostMapping("/list_vo")
    @Override
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }
    @GetMapping("/one_vo/{id}")
    @Override
    public ResponseResult findOneNew(@PathVariable("id") Integer id) {
        return wmNewsService.findOneNew(id);
    }
    /**
     * 文章审核成功
     * @param dto
     * @return
     */
    @PostMapping("/auth_pass")
    @Override
    public ResponseResult authPass(@RequestBody NewsAuthDto dto) {
        return wmNewsService.updateStatus(dto,WemediaContants.WM_NEWS_AUTH_PASS);
    }
    /**
     * 文章审核成功
     * @param dto
     * @return
     */
    @PostMapping("/auth_fail")
    @Override
    public ResponseResult authFail(@RequestBody NewsAuthDto dto) {
        return wmNewsService.updateStatus(dto,WemediaContants.WM_NEWS_AUTH_FAIL);
    }
}
